package com.elephant.core.designpattern.singleton;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author hai
 * @description
 * @date 10:59 2018/10/10
 */
public class Singleton2 {
    private static final AtomicReference<Singleton2> INSTANCE = new AtomicReference<Singleton2>();

    private Singleton2() {}

    public static Singleton2 getInstance() {
        for (;;) {
            Singleton2 singleton = INSTANCE.get();
            if (null != singleton) {
                return singleton;
            }

            singleton = new Singleton2();
            if (INSTANCE.compareAndSet(null, singleton)) {
                return singleton;
            }
        }
    }
}
